﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using CTIClassLibrary;

namespace CTIPayService
{
    /// <summary>
    /// CTIPayWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class CTIPayWebService : System.Web.Services.WebService
    {
        /// <summary>
        /// 退款
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="amount"></param>
        /// <returns>1 成功 其他信息 失败</returns>
        [WebMethod]
        public string Refund(string orderID, decimal amount)
        {
            try
            {
                //是否是订单原始编号？
                int intOrderID = int.Parse(orderID);
            }
            catch
            {
                WebService.TicketWebService service = new WebService.TicketWebService();
                orderID = service.GetOrderId(orderID);
            }

            BANK_CITIC citic = new BANK_CITIC();
            citic.PaymentRefund(orderID, amount);
            string info = "订单号：{0} 退款金额：{1}";
            info = string.Format(info, orderID, amount);

            if (string.IsNullOrEmpty(citic.ErrorMsg))
            {
                Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Context.Request.Url.AbsoluteUri)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Context.Request.UserHostAddress)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, "退款成功！" + info)
                    .Execute();
                return "1";
            }
            else
            {
                Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Context.Request.Url.AbsoluteUri)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Context.Request.UserHostAddress)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, citic.ErrorMsg)
                    .Execute();
                return citic.ErrorMsg;
            }
        }

        /// <summary>
        /// 撤消
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns>1 成功 其他信息 失败</returns>
        [WebMethod]
        public string Reversal(string orderID)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.PaymentReversal(orderID);
            string info = "订单号：{0}";
            info = string.Format(info, orderID);

            if (string.IsNullOrEmpty(citic.ErrorMsg))
            {
                Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Context.Request.Url.AbsoluteUri)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Context.Request.UserHostAddress)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, "撤销成功！" + info)
                    .Execute();
                return "1";
            }
            else
            {
                Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Context.Request.Url.AbsoluteUri)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Context.Request.UserHostAddress)
                    .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, citic.ErrorMsg)
                    .Execute();
                return citic.ErrorMsg;
            }
        }
    }
}
